package priv.mill.demo;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息生产者
 * @author mill
 *
 */
public class JMSProduct {
	private static final String brokerURL = "tcp://mill.one:61616";//ActiveMQConnection.DEFAULT_BROKER_URL;
	private static final String password = ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String userName = ActiveMQConnection.DEFAULT_USER;

	public static void main(String[] args) {
		/**
		 * 实例化连接工厂
		 */
		Connection createConnection = null;
		try {
			ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(userName, password, brokerURL);
			createConnection = connectionFactory.createConnection();//创建连接
			createConnection.start();//开启连接
			Session session = createConnection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);//创建会话
			Topic queue = session.createTopic("FirstTopic");//创建消息队列
			MessageProducer messageProducer = session.createProducer(queue);//创建消息生产者
			for (int i = 0; i < 10; i++) {//循环发送10条消息
				String string = "ActiveMq Topic"+i;
				Message msg = session.createTextMessage(string);//创建文本消息
				System.out.println(string);
				messageProducer.send(msg);//发送消息
			}
			session.commit();//提交消息
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(createConnection!=null)
			try {
				createConnection.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
		
	}
}
